<?php

// 商品列表

namespace app\shop\controller;

use think\Cookie;
use think\Request;
use think\Controller;
use app\shop\common\GuessGoods;
use app\shop\model\Shop as ShopModel;
use app\shop\model\Limited as LimitedModel;
use app\shop\model\GoodsType as GoodsTypeModel;
use app\shop\model\ShopGoods as ShopGoodsModel;
use app\shop\model\GroupGoods as GroupGoodsModel;
use app\shop\model\LimitedType as LimitedTypeModel;

class GoodsType extends Controller
{
    /**
     * 商品分类
     * @param Request $request
     * @param GoodsTypeModel $goodsType
     * @param ShopGoodsModel $shopGoods
     * @return mixed
     */
    public function index(Request $request, GoodsTypeModel $goodsType, ShopGoodsModel $shopGoods)
    {
        // 获取参数
        $inquire = $request->param();

        $category = '';
        $cate = $goodsType::get($inquire['type']);
        switch ($cate['count'])
        {
            case 1:
                $category = $goodsType->where(['status' => 1,'parenTid'=>$inquire['type']])->field('id,parenTid,title')->select();
                break;
            case 2:
                $category = $goodsType->where(['status' => 1,'parenTid'=>$cate['parenTid']])->field('id,parenTid,title')->select();
                break;
            default:
        }

        // 筛选条件
        $condition['state'] = ['eq',1];
        $condition['status'] = ['eq',1];
        // $condition['id'] = ['notin', $limit_str];
        if (!empty($inquire['title'])) $condition['title'] = ['like', '%' . $inquire['title'] . '%'];
        if (!empty($inquire['type'])) $condition['goods_str'] = ['like', '%,' . $inquire['type'] . ',%'];
        if (!empty($inquire['high'])) $condition['price'] = ['between',[$inquire['low'],$inquire['high']]];
        // 默认排序
        $sort = ['shop_sort' => 'desc', 'sort' => 'desc', 'create_time' => 'desc'];

        // 排序传参
        if (!empty($inquire['order'])) $sort = [$inquire['order'] => 'desc'];

        // 商品分类 对应的商品
        $data = $shopGoods->with('shop')->where($condition)->field('id,title,price,thumb_img,sale_number,rebate_percentage,hits,inventory,create_time,evaluate_number,member_id')
            ->order($sort)->paginate(20, false, ['query' => $inquire]);

        $limited_type = (new LimitedTypeModel())->where('id', 2)->value('end_time');

        foreach ($data as &$value) {
            $limit_info = (new LimitedModel())->where('goods_id', $value['id'])->value('price');
            if ($limit_info && $limited_type > time()) {
                $value['price'] = $limit_info;
            }
            $special_price = (new GroupGoodsModel())->where(['goods_id' => $value['id'], 'status' => 1])->value('price');
            if ($special_price) {
                $value['price'] = $special_price;
            }
        }

        // 商品总数
        $total = $shopGoods->where($condition)->count();

        // 猜你喜欢
        $guess_like = (new GuessGoods)->guessLike();


        return $this->fetch('', [
            'mate_head'  => '商品分类',
            'cates'      => $category,
            'data'       => $data,
            'total'      => $total,
            'guess_like' => $guess_like,
        ]);

    }


    /**
     * 搜索店铺
     * @param Request $request
     * @param ShopModel $shop
     * @return mixed
     */
    public function search_shop(Request $request, ShopModel $shop)
    {
        // 获取关键词
        $inquire = $request->param();

        // 搜索历史记录
        if (!empty($inquire['title'])) self::search_record($inquire['title']);

        // 筛选条件
        $condition['status'] = 1;
        if (!empty($inquire['title'])) $condition['title'] = ['like', '%' . $inquire['title'] . '%'];
        if (!empty($inquire['type'])) $condition['type_id'] = $inquire['type'];

        // 符合条件的店铺
        $result = $shop->with('Member,ShopGoods,ShopType')->where($condition)->field('title,type_id,member_id,logo,collect_number')->paginate(10, false, ['query' => $inquire]);

        // 销量 商品数
        foreach ($result as &$value) {
            $value['goods_count']  = (new ShopGoodsModel())->where(['member_id'=>$value['member_id'],'status'=>1,'state'=>1])->count();
        }

        // 符合店铺总数
        $count = $shop->where($condition)->count();

        // 猜你喜欢
        $guess_like = (new GuessGoods)->guessLike();



        return $this->fetch('', [
            'mate_head' => '店铺搜索',
            'data'      => $result,
            'title'     => $inquire['title'],
            'guess_like' => $guess_like,
            'count'     => $count
        ]);
    }



    public function search_goods(Request $request, ShopModel $shop, GoodsTypeModel $goodsType)
    {
        // 获取参数
        $inquire = Request::instance()->param();

        // 搜索历史记录
        if (!empty($inquire['title'])) self::search_record($inquire['title']);

        // 筛选条件
        $condition['state'] = ['eq',1];
        $condition['status'] = ['eq',1];
        if (!empty($inquire['title'])) $condition['title'] = ['like', '%' . $inquire['title'] . '%'];
        if (!empty($inquire['type'])) $condition['goods_str'] = ['like', '%,' . $inquire['type'] . ',%'];

        // 默认排序
        $sort = ['shop_sort' => 'desc', 'sort' => 'desc', 'create_time' => 'desc'];

        // 排序传参
        if (!empty($inquire['order'])) $sort = [$inquire['order'] => 'desc'];

        // 商品分类 对应的商品
        $result = (new ShopGoodsModel)->where($condition)->field('id,title,price,thumb_img,sale_number,state,rebate_percentage,hits,inventory,create_time,evaluate_number,member_id')
            ->order($sort)->paginate(28, false, ['query' => $inquire]);

        $limited_type = (new LimitedTypeModel())->where('id', 2)->value('end_time');

        foreach ($result as &$value) {
            $limit_info = (new LimitedModel())->where('goods_id', $value['id'])->value('price');
            if ($limit_info && $limited_type > time()) {
                $value['price'] = $limit_info;
            }
            $special_price = (new GroupGoodsModel())->where(['goods_id' => $value['id'], 'status' => 1])->value('price');
            if ($special_price) {
                $value['price'] = $special_price;
            }
        }

        // 商品总数
        $total = (new ShopGoodsModel)->where($condition)->count();

        // 猜你喜欢
        $guess_like = (new GuessGoods)->guessLike();


        return $this->fetch('', [
            'mate_head'  => '商品搜索',
            'data'       => $result,
            'total'      => $total,
            'guess_like' => $guess_like,
        ]);

    }

    public function search_record($info)
    {
        $new_info[] = $info;
        //是否含有搜索记录
        if (Cookie::has('search_record')) {
            $record = Cookie::get('search_record');
            ////合并记录&检测是否有重复的记录
            foreach ($record as $k => $value) {
                if ($info == $value) {
                    $new_info = $record;
                    break;
                } else {
                    if (count($record) >= 10) {
                        array_pop($record);  //删除最后一个记录
                    }
                    $new_info[] = $value;
                }
            }
        }
        //存入cookie
        Cookie::set('search_record', $new_info);
    }
}